package hpasDAO;

import hpasDataEntities.*;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;

/** 硬件数据访问类 **/
public class HardwareDAO
{
    private Connection conn;
    private HardwareItemDAO hardwareItemDAO;
    /** 创建数据库访问接口 **/
    public HardwareDAO()
    {
        try
        {
            connect();
        }
        catch (SQLException e)
        {
            JOptionPane.showMessageDialog(null,"数据库建立连接失败",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
        catch (ClassNotFoundException e)
        {
            JOptionPane.showMessageDialog(null,"找不到MySQL驱动程序",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
    }
    /** 创建连接 **/
    private void connect() throws SQLException, ClassNotFoundException
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hpas","root", "niuniu53");
    }
    /** 绑定商品查询接口 **/
    public void setHardwareItemDAO(HardwareItemDAO hardwareItemDAO)
    {
        this.hardwareItemDAO = hardwareItemDAO;
    }
    /** 搜索所有产品名包含keyword的方法，返回所有结果产品 **/
    public Vector<Hardware> getHardware(String keyword) throws SQLException
    {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(
                "select id, modelNo, productName, generalTypeId, detailedTypeId, brand_id, description, pic " +
                        "from hardware " +
                        "where productName like \"%" + keyword + "%\" " +
                        "order by id;");
        Vector<Hardware> result = new Vector<Hardware>();
        while (resultSet.next())
        {
            Hardware entry = new Hardware();
            entry.setId(resultSet.getInt(1));
            entry.setModelNo(resultSet.getString(2));
            entry.setProductName(resultSet.getString(3));
            entry.setGeneralType(resultSet.getInt(4));
            entry.setDetailedType(resultSet.getInt(5));
            entry.setBrand(resultSet.getInt(6));
            entry.setDescription(resultSet.getString(7));
            entry.setPicSrc(resultSet.getString(8));
            result.add(entry);
        }
        return result;
    }
    /** 搜索所有产品类型为hardware的硬件商品 **/
    public Vector<HardwareItem> getHardwareItems(Hardware hardware) throws SQLException
    {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(
                "select itemId, hardwareId, retailerId, price " +
                        "from hardwareitem " +
                        "where hardwareId=" + hardware.getId() +
                        " order by itemId;");
        Vector<HardwareItem> result = new Vector<HardwareItem>();
        while (resultSet.next())
        {
            HardwareItem entry = new HardwareItem();
            entry.setItemId(resultSet.getInt(1));
            entry.setHardwareId(resultSet.getInt(2));
            entry.setRetailerId(resultSet.getInt(3));
            entry.setPrice(resultSet.getBigDecimal(4));
            result.add(entry);
        }
        return result;
    }
    /** 使用id精准定位硬件 **/
    public Hardware locateHardwareById(int id) throws SQLException
    {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(
                "select id, modelNo, productName, generalTypeId, detailedTypeId, brand_id, description, pic " +
                        "from hardware " +
                        "where id=" + id);
        resultSet.next();
        Hardware entry = new Hardware();
        entry.setId(resultSet.getInt(1));
        entry.setModelNo(resultSet.getString(2));
        entry.setProductName(resultSet.getString(3));
        entry.setGeneralType(resultSet.getInt(4));
        entry.setDetailedType(resultSet.getInt(5));
        entry.setBrand(resultSet.getInt(6));
        entry.setDescription(resultSet.getString(7));
        entry.setPicSrc(resultSet.getString(8));
        return entry;
    }
    /** 添加新硬件 **/
    public void addHardware(Hardware hardware) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("insert into hardware " +
                "(modelNo,productName,generalTypeId,detailedTypeId,brand_id,description,pic) " +
                "values (" +
                "\'" + hardware.getModelNo() + "\'," +
                "\'" + hardware.getProductName() + "\'," +
                hardware.getGeneralType() + "," +
                hardware.getDetailedType() + "," +
                hardware.getBrand() + "," +
                "\'" + hardware.getDescription() + "\'," +
                "\'" + hardware.getPicSrc() + "\'" +
                ")");
    }
    /** 更新硬件信息 **/
    public void updateHardware(Hardware newHardware) throws SQLException
    {
        Hardware hardwareToUpdate = locateHardwareById(newHardware.getId());
        Statement statement = conn.createStatement();
        statement.executeUpdate("update hardware set " +
                "modelNo=\'" + newHardware.getModelNo() + "\'," +
                "productName=\'" + newHardware.getProductName() + "\'," +
                "generalTypeId=" + newHardware.getGeneralType() + "," +
                "detailedTypeId=" + newHardware.getDetailedType() + "," +
                "brand_id=" + newHardware.getBrand() + "," +
                "description=\'" + newHardware.getDescription() + "\'," +
                "pic=\'" + newHardware.getPicSrc() + "\' " +
                "where id=" + hardwareToUpdate.getId());
    }
    /** 删除指定硬件 **/
    public void removeHardware(Hardware hardware) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("delete from hardware where id=" + hardware.getId());
    }
    public static void main(String[] args) throws Exception
    {
        UIManager.setLookAndFeel("com.alee.laf.WebLookAndFeel");
        HardwareDAO dao = new HardwareDAO();
        Vector<Hardware> result = dao.getHardware("WD");
        for (Hardware h : result)
        {
            System.out.println(h.getProductName());
        }
        Hardware hh = new Hardware(result.get(2));
        hh.setProductName("WD BLACK P10 5TB 游戏移动硬盘 (WD40NMZW)");
        dao.updateHardware(hh);
        result = dao.getHardware("WD");
        for (Hardware h : result)
        {
            System.out.println(h.getProductName());
        }
        Hardware n = new Hardware();
        n.setProductName("希捷 酷鹰 (SkyHawk) 4TB 机械硬盘 监控用 (ST4000VX007)");
        n.setModelNo("ST4000VX007");
        n.setGeneralType(1);
        n.setDetailedType(1);
        n.setBrand(2);
        n.setDescription("希捷监控硬盘");
        dao.addHardware(n);
        result = dao.getHardware("");
        for (Hardware h : result)
        {
            System.out.println(h.getProductName());
        }
    }
}
